package com.fastdevelop.framework;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.fastdevelop.framework.base.BaseActivity;
import com.fastdevelop.framework.utils.Constant;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 *
 *
 * 明天来总结运行时权限的笔记
 */
public class MainActivity extends BaseActivity {

    private ListView lv_main;

    private List<String> mData;

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_main = (ListView) findViewById(R.id.lv_main);
        mData = new ArrayList<>();
    }

    public void requestSinglePermissionForReadExternalStorage(View view) {
        requestDangerPermissions(Constant.DangerPermissions.READ_EXTERNAL_STORAGE);
    }
    public void requestSinglePermissionForCamera(View view){
        requestDangerPermissions(Constant.DangerPermissions.CAMERA);
    }
    public void requestSinglePermissionForCallphoneAndBodysensors(View view){
        requestDangerPermissions(Constant.DangerPermissions.CALL_PHONE, Constant.DangerPermissions.BODY_SENSORS);
    }
    public void requestSinglePermissionForSendsmsAndRecordaudio(View view){
        requestDangerPermissions(Constant.DangerPermissions.SEND_SMS, Constant.DangerPermissions.RECORD_AUDIO);
    }


    @Override
    public void requestPermissionsCallback(Constant.RequestPermissionResult requestPermissionResult, Constant.RequestPermissionType requestPermissionType, final String[] permissions) {
        if(requestPermissionType.equals(Constant.RequestPermissionType.SINGLE)){
            Log.i(TAG,"正在申请"+permissions[0]+"权限");
            if(requestPermissionResult.equals(Constant.RequestPermissionResult.SUCCESS)){
                Log.i(TAG,permissions[0]+"权限申请成功，执行相应方法");
                if(isPermissionEquals(permissions, Constant.DangerPermissions.READ_EXTERNAL_STORAGE)){
                    Log.i(TAG,"正在执行：读取SDCard");
                    readSdCard();
                }else if(isPermissionEquals(permissions, Constant.DangerPermissions.CAMERA)){
                    Log.i(TAG,"正在执行：摄像头操作");
                    Toast.makeText(mActivity,"Open Camera",Toast.LENGTH_SHORT).show();
                }
            }else if(requestPermissionResult.equals(Constant.RequestPermissionResult.SHOULD_SHOW_RATIONALE)){
                Log.i(TAG,"应该温馨提示用户为何需要"+permissions[0]+"权限");
                showPermissionRationaleDialog(new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        againRequestPermission(permissions);
                    }
                });
            }else if(requestPermissionResult.equals(Constant.RequestPermissionResult.REJECT)){
                Log.i(TAG,"用户拒绝授权" +permissions[0]+ "权限");
                Toast.makeText(mActivity,"已拒绝授权",Toast.LENGTH_SHORT).show();
            }
        }else{
            StringBuffer stringBuffer = new StringBuffer("正在申请多个权限（");
            for(int i = 0;i<permissions.length;i++){
                stringBuffer.append(permissions[i] + "，");
                if(i == permissions.length - 1)
                    stringBuffer.replace(stringBuffer.length()-1,stringBuffer.length(),")");
            }
            Log.i(TAG,stringBuffer.toString());
            if(requestPermissionResult.equals(Constant.RequestPermissionResult.SUCCESS)){
                Log.i(TAG,"多个权限申请成功，执行相应方法");
                if(isPermissionEquals(permissions, Constant.DangerPermissions.CALL_PHONE) && isPermissionEquals(permissions, Constant.DangerPermissions.BODY_SENSORS)){
                    Log.i(TAG,"正在执行：CALL_PHONE，SENSORS");
                }
                if(isPermissionEquals(permissions, Constant.DangerPermissions.SEND_SMS)){
                    Log.i(TAG,"正在执行：SEND_SMS");
                }
                if(isPermissionEquals(permissions, Constant.DangerPermissions.RECORD_AUDIO)){
                    Log.i(TAG,"正在执行：RECORD_AUDIO");
                }
            }else if(requestPermissionResult.equals(Constant.RequestPermissionResult.REJECT)){
                Log.i(TAG,"多个权限中有一个被用户拒绝");
            }
        }
    }

    private void showPermissionRationaleDialog(DialogInterface.OnClickListener onClickListener) {
        new AlertDialog.Builder(mActivity)
                .setMessage("查看文件列表需要系统权限")
                .setPositiveButton("知道了", onClickListener)
                .show();
    }

    private void readSdCard() {
        mData.clear();
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            mData.add(files[i].getName());
        }
        lv_main.setAdapter(new ArrayAdapter<>(mActivity, android.R.layout.simple_list_item_1, mData));
    }
}
